﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using JiveMessenger.CustomControls;
using JiveMessenger.AvalonEdit;
using System.ComponentModel;
using JmShared;
using JiveMessenger.ViewModels;
using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.Models;
using System.IO;
using JiveMessenger.User;
using System.Windows.Media;
using JiveMessenger.Utilities;
using NLog;
using System.Windows.Media.Animation;
using System.Linq;

namespace JiveMessenger.Views
{
    /// <summary>
    /// Interaction logic for PrivateMessage.xaml
    /// </summary>
    public partial class ChatWindow : Window
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        public bool IsFlashing { get; set; }

        public ChatWindow(ViewModelChat vm)
        {

            InitializeComponent();

            var locator = (ViewModelLocator)Application.Current.Resources["Locator"];
            if (vm is ViewModelPrivateMessage)
                this.DataContext = new ViewModelPrivateMessageWindow(locator.CurrentClient, (ViewModelPrivateMessage)vm);
            else if (vm is ViewModelGroupChat)
                this.DataContext = new ViewModelGroupChatWindow(locator.CurrentClient, (ViewModelGroupChat)vm);


            Messenger.Default.Register<NotificationMessageAction<ChatWindow>>(this, msg =>
                {
                    switch (msg.Notification)
                    {
                        case "GetChatWindow":
                            if (msg.Sender == this.DataContext || msg.Sender.Equals(((ViewModelChatWindow)this.DataContext).CurrentActiveViewModel))
                                msg.Execute(this);
                            break;
                        case "GetChatWindowByID":
                            if (((ViewModelChatWindow)this.DataContext).CurrentActiveViewModel.ID == msg.Target.ToString())
                                msg.Execute(this);
                            break;
                    }
                });

            Messenger.Default.Register<NotificationMessage>(this, msg =>
                {
                    switch (msg.Notification)
                    {
                        case "Nudge":
                            {
                                if (this.IsActive)
                                {
                                    Storyboard board = this.Resources["ShakeWindowAnim"] as Storyboard;
                                    if (board != null)
                                        board.Begin();
                                }
                                break;
                            }
                        case "CloseWindow":
                            {
                                if (msg.Sender == this.DataContext || msg.Sender.Equals(((ViewModelPrivateMessageWindow)this.DataContext).CurrentActiveViewModel))
                                    this.Close();
                                break;
                            }
                        case "ActivateWindow":
                            {
                                if (msg.Sender == this.DataContext || msg.Sender.Equals(((ViewModelPrivateMessageWindow)this.DataContext).CurrentActiveViewModel))
                                    this.Activate();
                                break;
                            }
                    }
                });
        }
        public void SetActive()
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                this.Activate();

            }));
        }
        #region overrides
        protected override void OnClosing(CancelEventArgs e)
        {
            var viewModelLocator = (ViewModelLocator)Application.Current.Resources["Locator"];

            Messenger.Default.Unregister(this);
            ViewModelChatWindow vmWin = (ViewModelChatWindow)this.DataContext;
            vmWin.CurrentActiveViewModel.WriteLog();
            if (vmWin is ViewModelPrivateMessageWindow)
            {
                PrivateMessageBehavior behavior = viewModelLocator.CurrentClient.PrivateMessageBehavior;
                if (behavior == PrivateMessageBehavior.Windows)
                    viewModelLocator.ViewModelMainWindow.ChatViewModels.Remove(vmWin.CurrentActiveViewModel);
            }
            ViewModelChatWindow rem;
            if (!viewModelLocator.CurrentClient.OpenWindowViewModels.TryRemove(vmWin.CurrentActiveViewModel.ID, out rem))
                Logger.Error("Could not remove private message window from dictionary");
            vmWin.Cleanup();
            base.OnClosing(e);
        }

        protected override void OnActivated(EventArgs e)
        {
            // Application activated
            if (!this.IsFlashing) return;
            this.StopFlashingWindow();
            this.IsFlashing = false;
            base.OnActivated(e);
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (!this.IsFlashing) return;
            this.StopFlashingWindow();
            this.IsFlashing = false;
            base.OnMouseMove(e);
        }
        #endregion

        #region events
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SetActive();
            var locator = (ViewModelLocator)Application.Current.Resources["Locator"];
            ViewModelChatWindow vmp = (ViewModelChatWindow)this.DataContext;

            locator.CurrentClient.OpenWindowViewModels.GetOrAdd(vmp.CurrentActiveViewModel.ID, vmp);

        }

        private void Close_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Close();
        }
        private void ShakeWindowAnim_Completed(object sender, EventArgs e)
        {
            Storyboard board = this.Resources["ShakeWindowAnim"] as Storyboard;
            board.Remove();
        }

        #endregion

    }
}